package com.bwei.mypaot.home.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.bwei.mypaot.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/2/26.
 */

public class TypeAdapter extends RecyclerView.Adapter<TypeAdapter.MyViewHolder> {
    Context context;
    ArrayList<String> data;

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private MyViewHolder holder;

    public TypeAdapter(Context context, ArrayList<String> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =View.inflate(context,R.layout.type_item,null);

        holder = new MyViewHolder(view);
        //将创建的View注册点击事件
//        holder.tv_type.setOnClickListener((View.OnClickListener) context);


        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.tv_type.setText(data.get(position));
        Log.d("ccccccccccccc",data.get(position));
        //将创建的View注册点击事件
        holder.tv_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    //注意这里使用getTag方法获取数据
                    mOnItemClickListener.onItemClick(v,position);

                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder
    {
        TextView tv_type;


        public MyViewHolder(View view)
        {
            super(view);
            tv_type= (TextView) view.findViewById(R.id.tv_type);

        }


    }

    public  interface OnRecyclerViewItemClickListener {
        void onItemClick(View view , int position);
    }
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
